﻿using Newtonsoft.Json;
using System;
using WaitLess.Business.Data.Model.Antwerpen;
using WaitLess.Business.Data.Web.Interfaces;
using WaitLess.Business.Repositories.Interfaces;

namespace WaitLess.Business.Repositories
{
    public class DistrictshuisRepository : IDistrictshuisRepository
    {
        private const string DistrictshuizenJsonUrl = "http://api.antwerpen.be/v1/infrastructuur/districtshuis.json";
        private readonly IWebRequestor _webRequest;

        public DistrictshuisRepository(IWebRequestor webRequest)
        {
            if (webRequest == null) throw new ArgumentNullException("webRequest");

            _webRequest = webRequest;
        }

        public void GetAll(Action<Districtshuizen> receiveResult)
        {
            _webRequest.Download(DistrictshuizenJsonUrl, response => GetResult(response, receiveResult));
        }

        private void GetResult(string response, Action<Districtshuizen> receiveResult)
        {
            var districtshuizen = JsonConvert.DeserializeObject<Districtshuizen>(response);

            receiveResult(districtshuizen);
        }
    }
}